﻿package {
	
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.events.*;
	
	public class TextBoxProperty extends MovieClip {
		
		private var textbox:TextBox;
		
		private var fonts:DropDownList;
		private var txtSize:NumberSelectBox;
		private var xSelect:NumberSelectBox;
		private var ySelect:NumberSelectBox;
		private var wSelect:NumberSelectBox;
		private var hSelect:NumberSelectBox;
		private var colorText:TextField;
		private var strColor:String;		
		private var bgColorText:TextField;
		private var bgStrColor:String;		
		
		public function TextBoxProperty(tb:TextBox = null) {
			textbox = tb;
			setupSize();
			setupPositioning();
			setupFonts();
			setupColor();
		}
		
		//remove attachment from current textbox
		public function dislocate() { textbox = null; }
		
		public function getKeyValue(key:String):String {
			switch(key) {
				case "font": 
					return fonts.getSelectedValue();
				case "size":
					return String(txtSize.val);
				case "x":
					return String(xSelect.val);
				case "y":
					return String(ySelect.val);
				case "w":
					return String(wSelect.val);
				case "h":
					return String(hSelect.val);
				default:
					return "null";
			}
		}
		
		public function configure(textbox:TextBox) {
			this.textbox = textbox;
			fonts.setElement(textbox.getFont());
			txtSize.setSize(textbox.getFontSize());
			xSelect.setSize(textbox.getTextField().x);
			ySelect.setSize(textbox.getTextField().y);
			wSelect.setSize(textbox.getTextField().width);
			hSelect.setSize(textbox.getTextField().height);
		}
		
		private function changeFont(e:Event) {
			if (textbox == null) return;
			textbox.setFont(fonts.getSelectedValue());
		}
		
		private function changeSize(e:Event) {
			if (textbox == null) return;
			textbox.setSize(Number(txtSize.input.text));
		}
		
		private function changeX(e:Event) {
			if (textbox == null) return;
			textbox.setPos("x", xSelect.val)
		}
		
		private function changeY(e:Event) {
			if (textbox == null) return;
			textbox.setPos("y", ySelect.val)
		}
		
		private function changeW(e:Event) {
			if (textbox == null) return;
			textbox.setPos("w", wSelect.val)
		}
		
		private function changeH(e:Event) {
			if (textbox == null) return;
			textbox.setPos("h", hSelect.val)
		}
		
		private function setupFonts() {
			var allFonts:Array = Font.enumerateFonts(true);
			allFonts.sortOn("fontName", Array.CASEINSENSITIVE);
			var fontNames:Array = new Array(allFonts.length);
			for (var i:int = 0; i < allFonts.length; i++) fontNames[i] = Font(allFonts[i]).fontName;
			fonts = new DropDownList(fontNames, 120, 70);
			addChild(fonts);
			fonts.addEventListener(Event.CHANGE, changeFont);
		}
		
		private function setupSize() {
			txtSize = new NumberSelectBox(330, 40, 30, 20, 72, 8, 2, 12);
			addChild(createLabel("Size:", 302, 40, 30, 20));
			addChild(txtSize.input);
			txtSize.addEventListener(Event.CHANGE, changeSize);
		}
		
		private function setupPositioning() {
			xSelect = new NumberSelectBox(135, 40, 30, 20, 1024, 100, 5, 100);
			ySelect = new NumberSelectBox(180, 40, 30, 20, 768, 100, 5, 100);
			wSelect = new NumberSelectBox(225, 40, 30, 20, 668, 80, 5, 100);
			hSelect = new NumberSelectBox(270, 40, 30, 20, 924, 80, 5, 100);
			addChild(createLabel("X:", 120, 40, 20, 20));
			addChild(xSelect.input);
			addChild(createLabel("Y:", 165, 40, 20, 20));
			addChild(ySelect.input);
			addChild(createLabel("W:", 210, 40, 20, 20));
			addChild(wSelect.input);
			addChild(createLabel("H:", 255, 40, 20, 20));
			addChild(hSelect.input);
			xSelect.addEventListener(Event.CHANGE, changeX);
			ySelect.addEventListener(Event.CHANGE, changeY);
			wSelect.addEventListener(Event.CHANGE, changeW);
			hSelect.addEventListener(Event.CHANGE, changeH);
		}
		
		private function setupColor() {
			colorText = new TextField();
			colorText.text = "Black";
			strColor = "Black";
			colorText.background = true; colorText.backgroundColor = 0xFFFFFF;
			colorText.border = true; colorText.borderColor = 0x000000;		
			colorText.x = 360; colorText.y = 70;
			colorText.width = 60; colorText.height = 20;
			colorText.type = TextFieldType.INPUT;
			addChild(createLabel("Text color:", 305, 70, 55, 20));
			addChild(colorText);
			colorText.addEventListener(KeyboardEvent.KEY_DOWN, colorListener);
			
			bgColorText = new TextField();
			bgColorText.text = "Clear";
			bgStrColor = "Clear";
			bgColorText.background = true; bgColorText.backgroundColor = 0xFFFFFF;
			bgColorText.border = true; bgColorText.borderColor = 0x000000;		
			bgColorText.x = 480; bgColorText.y = 70;
			bgColorText.width = 60; bgColorText.height = 20;
			bgColorText.type = TextFieldType.INPUT;
			addChild(createLabel("BG color:", 425, 70, 55, 20));
			addChild(bgColorText);
			bgColorText.addEventListener(KeyboardEvent.KEY_DOWN, bgColorListener);
		}
		
		private function bgColorListener(event:KeyboardEvent){
			var converter:ColorConverter = new ColorConverter();
		    if(event.charCode == 13){ //If Enter
				var tempColor = converter.convert(bgColorText.text);
			    if (tempColor != null) {
					if (textbox != null) textbox.setBgColor(tempColor, tempColor == -1 ? true:false);
					bgStrColor = bgColorText.text;
				}
			}
		}
		
		private function colorListener(event:KeyboardEvent){
			var converter:ColorConverter = new ColorConverter();
		    if(event.charCode == 13){ //If Enter
				var tempColor = converter.convert(colorText.text);
			    if (tempColor != null) {
					if (textbox != null) textbox.setColor(tempColor);
					strColor = colorText.text;
				}
			}
		}
		
		private function createLabel(str:String, X:int, Y:int, W:int, H:int, color:uint = 0x000000):TextField {
			var tf:TextField = new TextField();
			tf.text = str;
			tf.x = X; tf.y = Y; tf.width = W; tf.height = H;
			var format:TextFormat = new TextFormat();
			format.color = color;
			tf.setTextFormat(format);
			return tf;			
		}
	}
}